package com.koron.ticket.work.workdescription.service;

import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import com.koron.ticket.utils.StringUtils;
import com.koron.ticket.work.workdescription.bean.WorkDescription;
import com.koron.ticket.work.workdescription.mapper.WorkDescriptionMapper;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WorkDescriptionService {

    private EamUser getCurrUser() {
        return ThreadLocalContext.get();
    }

    @TaskAnnotation("insert")
    public Response insert(SessionFactory factory, WorkDescription workDescription) {
        WorkDescriptionMapper workDescriptionMapper = factory.getMapper(WorkDescriptionMapper.class);
        String workTicketId = workDescription.getWorkTicketId();

        // 1. 根据workTicketId判断是否能够填写备注
        if (StringUtils.isEmpty(workTicketId)) {
            return Response.fail("该工作票还未保存，暂时无法填写备注");
        }
        // 2. 生成id
        workDescription.setId(CodeTools.getCode32());
        // 3. 保存用户信息
        workDescription.setCreateUserId(getCurrUser().getId());
        workDescription.setCreateUserName(getCurrUser().getName());
        workDescription.setCreateTime(new Date());
        workDescriptionMapper.insert(workDescription);

        return Response.success("备注保存成功");
    }

    @TaskAnnotation("getListByWorkTicketId")
    public Response getListByWorkTicketId(SessionFactory factory, String workTicketId) {
        WorkDescriptionMapper workDescriptionMapper = factory.getMapper(WorkDescriptionMapper.class);

        // 1. 根据workTicketId判断是否有备注
        if (StringUtils.isEmpty(workTicketId)) {
            return Response.fail("当前工作票没有备注信息");
        }
        // 2. 根据workTicketId获取对应的数据
        List<WorkDescription> descriptionList = workDescriptionMapper.getListByWorkTicketId(workTicketId);

        return Response.success(descriptionList);
    }
}
